package com.thenmorethings.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.thenmorethings.dao.BaseDao;

/**
 * BaseDao的实现
 * @author ChenHao.Ye
 *
 * 时间:2016年5月26日下午12:48:50
 */
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T>{

	Class<T> clazz = null;
	

	
	public BaseDaoImpl()
	{
		//获取T的类型
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
		clazz = (Class<T>)pt.getActualTypeArguments()[0];
	}
	
	@Override
	public void save(T entity) {
		
		getHibernateTemplate().save(entity);
		
	}

	@Override
	public void update(T entity) {
		
		getHibernateTemplate().update(entity);
	}

	@Override
	public void deleteById(Serializable id) {
		
		getHibernateTemplate().delete(findObjectById(id));
	}

	@Override
	public T findObjectById(Serializable id) {
		
		return getHibernateTemplate().get(clazz, id);
		
	}

	@Override
	public List<T> findObjects() {
		Query query = getSession().createQuery("FROM "+clazz.getSimpleName());
		/*Session session = getSession();
		String proAll = "{call protestfind()}";
		SQLQuery query = session.createSQLQuery(proAll);*/
		return query.list();
	}

	

}
